package com.sunshine.lnuplus.internet

import android.content.Context
import androidx.fragment.app.FragmentManager
import com.google.gson.Gson
import com.google.gson.JsonElement
import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
import com.sunshine.lnuplus.callback.LoadQingGuoSchoolListCallBack
import com.sunshine.lnuplus.callback.LoadQingGuoXueQiListCallBack
import com.sunshine.lnuplus.callback.LoginCallBack
import com.sunshine.lnuplus.model.QingGuoLessonBean
import com.sunshine.lnuplus.model.QingGuoSchoolBean
import com.sunshine.lnuplus.model.QingGuoUserBean
import com.sunshine.lnuplus.model.QingGuoXueQiBean
import com.sunshine.lnuplus.model.room.TimetableRepository
import com.sunshine.lnuplus.utils.LoadLessonUtils
import com.sunshine.lnuplus.utils.QingGuoUtils
import org.jsoup.Connection
import org.jsoup.Jsoup
import java.lang.Exception

/**
 * Created by sunshine on 2020/4/6
 */
object LoadQingGuoLesson {

    //请求接口
    private val URL = "http://www.xiqueer.com:8080/manager/wap/wapController.jsp"

    fun loadSchoolList(qingGuoSchoolListCallBack: LoadQingGuoSchoolListCallBack){
        object : Thread(){
            override fun run() {
                super.run()
                val schoolList = ArrayList<QingGuoSchoolBean>()
                try {
                    val conn = Jsoup.connect(URL)
                    conn.method(Connection.Method.POST)
                    conn.data(QingGuoUtils.getSchoolListParams())
                    val json = JsonParser.parseString(conn.execute().body().trim()).asJsonArray
                    json.forEach {
                        schoolList.add(Gson().fromJson(it, object : TypeToken<QingGuoSchoolBean>(){}.type))
                    }
                }catch (e: Exception){

                }
                qingGuoSchoolListCallBack.onSuccess(schoolList)
            }
        }.start()
    }

    fun loadXueQiList(xxdm: String, user: String, pw: String, loadQingGuoXueQiListCallBack: LoadQingGuoXueQiListCallBack){
        object : Thread(){
            override fun run() {
                super.run()
                try {
                    val conn = Jsoup.connect(URL)
                    conn.method(Connection.Method.POST)
                    conn.data(QingGuoUtils.getLoginParams(xxdm, null, null, null, user, pw))
                    val json = JsonParser.parseString(conn.execute().body().trim())
                    val userBean: QingGuoUserBean = Gson().fromJson(json, object : TypeToken<QingGuoUserBean>(){}.type)
                    if(userBean.flag == "0"){
                        val conn2 = Jsoup.connect(URL)
                        conn2.method(Connection.Method.POST)
                        conn2.data(QingGuoUtils.getYearListParams(userBean))
                        val json2 = JsonParser
                            .parseString(conn2.execute().body().trim())
                            .asJsonObject
                            .getAsJsonArray("xnxq")
                        val xqList = ArrayList<QingGuoXueQiBean>()
                        json2.forEach {
                            xqList.add(Gson().fromJson(it, object : TypeToken<QingGuoXueQiBean>(){}.type))
                        }
                        loadQingGuoXueQiListCallBack.onSuccess(xqList, userBean)
                    }else{
                        loadQingGuoXueQiListCallBack.onFail(userBean.msg)
                    }
                }catch (e: Exception){
                    loadQingGuoXueQiListCallBack.onFail(e.toString())
                }
            }
        }.start()
    }

    fun loadQingGuoLesson(dm: String, userBean: QingGuoUserBean, loginCallBack: LoginCallBack, context: Context, fragmentManager: FragmentManager){
        object : Thread(){
            override fun run() {
                super.run()
                try {
                    val qingGuoLessonList = ArrayList<QingGuoLessonBean>()
                    for(week in 1..30){
                        val conn = Jsoup.connect(URL)
                        conn.method(Connection.Method.POST)
                        conn.data(QingGuoUtils.getCourseParams(null, null, week.toString(), dm, userBean))
                        val json = JsonParser
                            .parseString(conn.execute().body().trim())
                            .asJsonObject
                        for(day in 1..7){
                            try {
                                json.getAsJsonArray("week$day").forEach {
                                    val lessonBean: QingGuoLessonBean = Gson().fromJson(it, object : TypeToken<QingGuoLessonBean>(){}.type)
                                    lessonBean.dayOfWeek = day
                                    qingGuoLessonList.add(lessonBean)
                                }
                            }catch (e: Exception){}
                        }
                    }
                    LoadLessonUtils.initQingGuoLesson(TimetableRepository(context), qingGuoLessonList, loginCallBack, context, fragmentManager)
                }catch (e: Exception){
                    loginCallBack.onFail(e.toString())
                }

            }
        }.start()
    }
}